package com.usoft;

import akka.dispatch.ExecutionContexts;
import akka.dispatch.Futures;
import akka.dispatch.OnSuccess;
import akka.japi.Function;
import scala.concurrent.ExecutionContextExecutorService;
import scala.concurrent.Future;

import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;

/**
 * Futures.traverse
 * Created by liyanxin on 2015/1/9.
 */
public class ComposeFuture2 {

    public static void main(String args[]) {
        final ExecutionContextExecutorService ec = ExecutionContexts.
                fromExecutorService(Executors.newCachedThreadPool());

        //Just a sequence of Strings
        Iterable<String> listStrings = Arrays.asList("a", "b", "c");
        Future<Iterable<String>> futureResult = Futures.traverse(listStrings,
                new Function<String, Future<String>>() {
                    public Future<String> apply(final String r) {
                        return Futures.future(new Callable<String>() {
                            public String call() {
                                return r.toUpperCase();
                            }
                        }, ec);
                    }
                }, ec);
        //Returns the sequence of strings as upper case
        futureResult.onSuccess(new OnSuccess<Iterable<String>>() {
            @Override
            public void onSuccess(Iterable<String> result) throws Throwable {
                for (String s : result) {
                    System.out.println(s);
                }
            }
        }, ec);
    }
}
